package com.lxb.myforum.pojo

import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.JoinTable
import javax.persistence.ManyToMany
import javax.persistence.Table

import com.fasterxml.jackson.annotation.JsonIgnore

/**
 * 一个资源对应多个权限
 * 一个权限对应多个资源
 * @author lxb
 */
@Entity
@Table(name = "t_resource")
data class Resource (
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long? = null,

    @Column(name = "name")
    var name: String? = null,

    @Column(name = "url")
    var url: String? = null,

    @Column(name = "url_key")
    var urlKey: String? = null,

    @Column(name = "enabled")
    var isEnabled: Boolean = true,

    @Column
    var remark: String? = null,

    @ManyToMany(targetEntity = Authority::class, fetch = FetchType.EAGER)
    @JoinTable(
            name = "t_authority_resource",
            joinColumns = [JoinColumn(name = "resource_id", referencedColumnName = "id")],
            inverseJoinColumns = [JoinColumn(name = "authority_id", referencedColumnName = "id")]
    )
    @get:JsonIgnore
    var authorities: List<Authority>? = null
) {
    constructor(resId: Long?): this(id = resId)
}
